package com.etoak.web.sys_menu.entity;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author by 孙有泽
 * @Date 2025/2/20 21:10
 * @Description
 */
public class MakeMenuTree {
    public static List<SysMenu> makeTree(List<SysMenu> menulist, String pid){
        //组装树
        System.out.println(menulist);
        List<SysMenu> list = new ArrayList<>();
        Optional.ofNullable(menulist).orElse(new ArrayList<>()).stream().filter(item-> item!=null && item.getPid().equals(pid)).forEach(item->{
            System.out.println(item);
            SysMenu sysMenu = new SysMenu();
            BeanUtils.copyProperties(item,sysMenu);
            sysMenu.setLabel(item.getTitle());
            sysMenu.setValue(item.getId());
            List<SysMenu> children = makeTree(menulist, item.getId());
            sysMenu.setChildren(children);
            list.add(sysMenu);
        });
        return list;


    }
    public static List<RouterVo> makeRouterTree(List<SysMenu> menulist, String pid){
        ArrayList<RouterVo> objects = new ArrayList<>();
        Optional.ofNullable(menulist).orElse(new ArrayList<>())
                .stream().filter(item-> item!=null && item.getPid().equals(pid)).forEach(item-> {
                    RouterVo routerVo = new RouterVo();
                    routerVo.setName(item.getName());
                    routerVo.setPath(item.getPath());
                    List<RouterVo> children = makeRouterTree(menulist, item.getId());
                    routerVo.setChildren(children);
                    if (item.getPid().equals("0")) {
                        routerVo.setComponent("Layout");
                        if (item.getType().equals("1")) {
                            routerVo.setRedirect(item.getPath());
                            ArrayList<RouterVo> objects1 = new ArrayList<>();
                            RouterVo routerVo1 = new RouterVo();
                            routerVo1.setPath(item.getPath());
                            routerVo1.setComponent(item.getUrl());
                            routerVo1.setName(item.getName());
                            routerVo1.setMeta(routerVo1.new Meta(item.getTitle(), item.getIcon(), item.getCode().split(",")));
                            objects1.add(routerVo1);
                            routerVo.setChildren(objects1);
                            routerVo.setPath(item.getPath() + "parent");
                            routerVo.setName(item.getName() + "parent");
                        }

                    } else {
                        routerVo.setComponent(item.getUrl());
                    }
                    routerVo.setMeta(routerVo.new Meta(item.getTitle(), item.getIcon(), item.getCode().split(",")));
                    objects.add(routerVo);
                });
            return objects;
    }
}
